import axios from 'axios'
import { InternalAxiosRequestConfig } from 'axios'
const baseURL = import.meta.env.VITE_VUE_APP_API_URL
import { useUserStore } from '../store/user'
import pinia from '../store/pinia'
import { storeToRefs } from 'pinia'
const store = useUserStore(pinia)
let { spin } = storeToRefs(store)
/**
 * @description: 创建axios实例
 */
const instance = axios.create({
  baseURL,
  timeout: 4000
})

/**
 * @description: 创建axios的请求拦截器
 */
instance.interceptors.request.use((config:InternalAxiosRequestConfig) => {
    let token = store.token
    if (token) {
        config.headers.Authorization = `Bearer ${token}`
    }
    spin.value = true
    return config
})

/**
 * @description: 创建axios的响应拦截器
 */
instance.interceptors.response.use(((config: any)=> {
    spin.value = false
    return config
}), _ => {
    spin.value = false
})

export default instance